package com.example.qq_demo;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;


public class Register_Activity extends AppCompatActivity {
        private EditText name,pwd;
        private Button btn_register;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }
    public void initView()
    {
        name=findViewById(R.id.name);
        pwd=findViewById(R.id.pwd);
        btn_register=findViewById(R.id.btn_register);

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BufferedWriter bw=null;
                FileOutputStream fos=null;
                try {
                     fos=openFileOutput("text.txt", Context.MODE_PRIVATE);
                    bw=new BufferedWriter(new OutputStreamWriter(fos));
                    String str=name.getText().toString()+":"+pwd.getText().toString();
                    bw.write(str);
                    bw.flush();
                    Toast.makeText(Register_Activity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                finally {
                    if(bw!=null)
                    {
                        try {
                            bw.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fos!=null)
                    {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                Intent intent=new Intent(Register_Activity.this,Login_Activity.class);
                startActivity(intent);
            }
        });
    }
}

